﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Uco.Models;
using System.Diagnostics;
using System.Net.Mail;
using System.Configuration;
using System.Text;
using System.Reflection;
using System.Globalization;
using System.Web.Configuration;
using Telerik.Web.Mvc.UI;
using System.Xml.Serialization;
using System.IO;

namespace Uco.Infrastructure
{

    public static partial class SF
    {
        public static AbstractPage LoadPage(string SeoUrlName)
        {
            if (!File.Exists(HttpContext.Current.Server.MapPath("~/App_Data/pages/" + SeoUrlName + ".xml")))
            {
                if (SeoUrlName == "root")
                {
                    LanguagePage root = new LanguagePage()
                    {
                        ChangeTime = DateTime.Now,
                        ID = 1,
                        ParentID = 0,
                        SeoUrlName = "root",
                        ShowInMenu = true,
                        ShowInSitemap = true,
                        Title = "root",
                        Order = 0,
                        Visible = true
                    };
                    root.Save();
                    return root;
                }
                return null;
            }

            Type[] t = SF.GetAbstractPageChildClasses().ToArray();
            XmlSerializer AbstractPageSerializer = new XmlSerializer(typeof(AbstractPage), t);
            FileStream AbstractPageFileStream = new FileStream(HttpContext.Current.Server.MapPath("~/App_Data/pages/" + SeoUrlName + ".xml"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
            AbstractPage AbstractPageItem = (AbstractPage)AbstractPageSerializer.Deserialize(AbstractPageFileStream);
            AbstractPageFileStream.Close();
            AbstractPageFileStream.Dispose();
            return AbstractPageItem;
        }

        public static Settings LoadSettings()
        {
            if (!File.Exists(HttpContext.Current.Server.MapPath("~/App_Data/data/Settings.xml")))
            {
                Settings item = new Settings()
                {
                    AdminEmail = "info@uco.co.il",
                    Error404 = "",
                    Footer = "",
                    FotterHtml = "",
                    HeaderHtml = ""
                };
                SF.SaveSettings(item);
                return item;
            }
            XmlSerializer classSerializer = new XmlSerializer(typeof(Settings));
            FileStream classFileStream = new FileStream(HttpContext.Current.Server.MapPath("~/App_Data/data/Settings.xml"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
            return (Settings)classSerializer.Deserialize(classFileStream);
        }

        public static string SaveSettings(Settings settings)
        {
            try
            {
                XmlSerializer classSerializer = new XmlSerializer(typeof(Settings));
                StreamWriter classComponentWriter = new StreamWriter(HttpContext.Current.Server.MapPath("~/App_Data/data/Settings.xml"));
                classSerializer.Serialize(classComponentWriter, settings);
                classComponentWriter.Close();
            }
            catch(Exception e) { return e.Message; }
            return string.Empty;
        }

        public static List<TextComponent> LoadTextComponents()
        {
            if (!File.Exists(HttpContext.Current.Server.MapPath("~/App_Data/data/TextComponents.xml")))
            {
                List<TextComponent> l = new List<TextComponent>();
                SF.SaveTextComponents(l);
                return l;
            }

            XmlSerializer classSerializer = new XmlSerializer(typeof(List<TextComponent>));
            FileStream classFileStream = new FileStream(HttpContext.Current.Server.MapPath("~/App_Data/data/TextComponents.xml"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
            return (List<TextComponent>)classSerializer.Deserialize(classFileStream);
        }

        public static string SaveTextComponents(List<TextComponent> items)
        {
            XmlSerializer classSerializer = new XmlSerializer(typeof(List<TextComponent>));
            StreamWriter classComponentWriter = new StreamWriter(HttpContext.Current.Server.MapPath("~/App_Data/data/TextComponents.xml"));
            classSerializer.Serialize(classComponentWriter, items);
            classComponentWriter.Close();
            return string.Empty;
        }

        public static List<GalleryBanner> LoadGalleryBanners()
        {
            if (!File.Exists(HttpContext.Current.Server.MapPath("~/App_Data/data/GalleryBanners.xml")))
            {
                List<GalleryBanner> l = new List<GalleryBanner>();
                SF.SaveGalleryBanners(l);
                return l;
            }

            XmlSerializer classSerializer = new XmlSerializer(typeof(List<GalleryBanner>));
            FileStream classFileStream = new FileStream(HttpContext.Current.Server.MapPath("~/App_Data/data/GalleryBanners.xml"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite);

            return (List<GalleryBanner>)classSerializer.Deserialize(classFileStream);
        }

        public static string SaveGalleryBanners(List<GalleryBanner> items)
        {
            XmlSerializer classSerializer = new XmlSerializer(typeof(List<GalleryBanner>));
            StreamWriter classComponentWriter = new StreamWriter(HttpContext.Current.Server.MapPath("~/App_Data/data/GalleryBanners.xml"));
            classSerializer.Serialize(classComponentWriter, items);
            classComponentWriter.Close();
            return string.Empty;
        }

        public static List<Contact> LoadContacts()
        {
            if (!File.Exists(HttpContext.Current.Server.MapPath("~/App_Data/data/Contacts.xml")))
            {
                List<Contact> l = new List<Contact>();
                SF.SaveContacts(l);
                return l;
            }
            XmlSerializer classSerializer = new XmlSerializer(typeof(List<Contact>));
            FileStream classFileStream = new FileStream(HttpContext.Current.Server.MapPath("~/App_Data/data/Contacts.xml"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
            List<Contact> l0 = (List<Contact>)classSerializer.Deserialize(classFileStream);
            classFileStream.Close();
            classFileStream.Dispose();
            return l0;
        }

        public static string SaveContacts(List<Contact> items)
        {
            XmlSerializer classSerializer = new XmlSerializer(typeof(List<Contact>));
            StreamWriter classComponentWriter = new StreamWriter(HttpContext.Current.Server.MapPath("~/App_Data/data/Contacts.xml"));
            classSerializer.Serialize(classComponentWriter, items);
            classComponentWriter.Close();
            classComponentWriter.Dispose();
            return string.Empty;
        }

        public static List<UcoMenuItem> LoadUcoMenuItems()
        {
            if (!File.Exists(HttpContext.Current.Server.MapPath("~/App_Data/data/UcoMenuItem.xml")))
            {
                RefreshMenuRepository();
                return new List<UcoMenuItem>();
            }
            else
            {
                XmlSerializer classSerializer = new XmlSerializer(typeof(List<UcoMenuItem>));
                FileStream classFileStream = new FileStream(HttpContext.Current.Server.MapPath("~/App_Data/data/UcoMenuItem.xml"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                return (List<UcoMenuItem>)classSerializer.Deserialize(classFileStream);
            }
        }

        public static void SaveUcoMenuItems(List<UcoMenuItem> items)
        {
            XmlSerializer classSerializer = new XmlSerializer(typeof(List<UcoMenuItem>));
            StreamWriter classComponentWriter = new StreamWriter(HttpContext.Current.Server.MapPath("~/App_Data/data/UcoMenuItem.xml"));
            classSerializer.Serialize(classComponentWriter, items);
            classComponentWriter.Close();
            classComponentWriter.Dispose();
        }

    }
}